// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import {sync} from 'vuex-router-sync';
import FastClick from 'fastclick';
import store from './store/index';
import router from './router/index';
import App from './App';
import utils from './commons/utils';

FastClick.attach(document.body);


sync(store, router);

// 登录检查
const LOGIN_CHECK_EXCLUDE_PATHS = ['/login', '/register', '/forget', '/reset', '/protocol']  // 登录检查排除的路径
router.beforeEach(function (to, from, next) {
  let hasExclude = false
  for (let excludePath of LOGIN_CHECK_EXCLUDE_PATHS) {
    if (to.path.indexOf(excludePath) != -1) {
      if (to.path.indexOf('/login') != -1 && utils.isLogin()) {
        next(false)
      } else {
        next()
      }
      hasExclude = true
      break
    }
  }

  if (hasExclude) {
    return
  }

  if (utils.isLogin()) {
    next()
  } else {
    let route = {
      path: '/login',
      query: {
        from: to.path
      }
    }
    next(route)
  }
})

Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
  store,
  router,
  render: h => h(App),
}).$mount('#app-box');
